package com.cnap.multiclouldmgmt.util;

import io.fabric8.kubernetes.api.model.Quantity;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
/**
 * @author dyq
 * 工具类
 */
public class QuantityUtils {

    /**
     * 一核= 1000m 1m = 1000*1000n
     * @param cpuQuantity
     * @return
     * @throws ParseException
     */
    public static long countCpuUseToN(Quantity cpuQuantity) throws ParseException {

        long cpuUse = 0L;
        //格式为n，直接获取
        if (StringUtils.equals(cpuQuantity.getFormat(),"n")){
            cpuUse += Long.parseLong(cpuQuantity.getAmount());
        }

        //m = 1000*1000n
        if (StringUtils.equals(cpuQuantity.getFormat(),"m")){
            cpuUse += Long.parseLong(cpuQuantity.getAmount())*1000*1000;
        }

        //无单位
        if (StringUtils.isEmpty(cpuQuantity.getFormat())){
            cpuUse += Long.parseLong(cpuQuantity.getAmount())*1000*1000*1000;
        }

        return cpuUse;
    }

    /**
     *
     * @param memQuantity
     * @return
     */
    public static long countMemUseToKi(Quantity memQuantity) {

        long memUse = 0L;

        if (StringUtils.equals(memQuantity.getFormat(),"Ki")){
            memUse += Long.parseLong(memQuantity.getAmount());
        }

        if (StringUtils.equals(memQuantity.getFormat(),"K")){
            memUse += Long.parseLong(memQuantity.getAmount());
        }

        if (StringUtils.equals(memQuantity.getFormat(),"M")){
            memUse += Long.parseLong(memQuantity.getAmount())*1000;
        }

        if (StringUtils.equals(memQuantity.getFormat(),"Mi")){
            memUse += Long.parseLong(memQuantity.getAmount())*1024;
        }

        if (StringUtils.equals(memQuantity.getFormat(),"G")){
            memUse += Long.parseLong(memQuantity.getAmount())*1000*1000;
        }

        if (StringUtils.equals(memQuantity.getFormat(),"Gi")){
            memUse += Long.parseLong(memQuantity.getAmount())*1024*1024;
        }

        return memUse;
    }

    /**
     *
     * @param cpuUseByN
     * @return
     */
    public static String changeCpuNtoM(Long cpuUseByN) {

        long nTom = 1000 * 1000L;

        return String.format("%.2f m", (float) cpuUseByN/nTom);

    }

    /**
     *
     * @param cpuUseByN
     * @return
     */
    public static int changeCpuNtoMInt(Long cpuUseByN) {

        long nTom = 1000 * 1000L;

        return  (int)(cpuUseByN/nTom);

    }

    /**
     *
     * @param cpuUseByN
     * @return
     */
    public static int changeCpuNtoNum(Long cpuUseByN) {

        long nTom = 1000 * 1000 * 1000L;

        return (int)(cpuUseByN/nTom);

    }

    /**
     *
     * @param cpuUseByN
     * @return
     */
    public static double changeCpuNtoNumDouble(Long cpuUseByN) {

        long nTom = 1000 * 1000 * 1000L;

        return Double.parseDouble(String.format("%.2f", (float) cpuUseByN/nTom));


    }

    /**
     *
     * @param memUseByKi
     * @return
     */
    public static Double changeMemKitoMiDouble(Long memUseByKi) {

        long kiToMi = 1024;

        return Double.parseDouble(String.format("%.2f", (float) memUseByKi/kiToMi));

    }

    /**
     *
     * @param memUseByKi
     * @return
     */
    public static String changeMemKitoMi(Long memUseByKi) {

        long kiToMi = 1024;

        return String.format("%.2f Mi", (float) memUseByKi/kiToMi);

    }

    /**
     *
     * @param memUseByKi
     * @return
     */
    public static int changeMemKitoMiNoMi(Long memUseByKi) {

        long kiToMi = 1024;

        return (int)(memUseByKi/kiToMi);

    }

    /**
     *
     * @param cpuUse
     * @return
     */
    public static String changeCpuNtoNumStr(long cpuUse) {


        long nTom = 1000 * 1000 * 1000L;

        return String.format("%.2f", (float) cpuUse/nTom);
    }

    /**
     *
     * @param memTotal
     * @return
     */
    public static String changeMemKitoGiStr(long memTotal) {

        long kiToGi = 1024 *1024L;
        return String.format("%.2f", (float) memTotal/kiToGi);
    }

    /**
     *
     * @param memTotal
     * @return
     */
    public static int changeMemKitoGi(long memTotal) {

        long kiToGi = 1024 *1024L;

        return (int)(memTotal/kiToGi);
    }

    /**
     *
     * @param disk
     * @return
     */
    public static int changeDiskBtoGB(long disk) {

        long BToGi = 1024 *1024 * 1024L;

        return (int)(disk/BToGi);
    }

    /**
     *
     * @param memUse
     * @return
     */
    public static String changeMemKiToGiStr(long memUse) {

        long nTom = 1024 *1024L;

        return String.format("%.2f", (float) memUse/nTom);
    }

    /**
     *
     * @param quantity
     * @return
     */
    public static long countStorageToKi(Quantity quantity) {

        long memUse = 0L;

        if (StringUtils.equals(quantity.getFormat(),"Ki")){
            memUse += Long.parseLong(quantity.getAmount());
        }

        if (StringUtils.equals(quantity.getFormat(),"K")){
            memUse += Long.parseLong(quantity.getAmount());
        }

        if (StringUtils.equals(quantity.getFormat(),"M")){
            memUse += Long.parseLong(quantity.getAmount())*1000;
        }

        if (StringUtils.equals(quantity.getFormat(),"Mi")){
            memUse += Long.parseLong(quantity.getAmount())*1024;
        }

        if (StringUtils.equals(quantity.getFormat(),"G")){
            memUse += Long.parseLong(quantity.getAmount())*1000*1000;
        }

        if (StringUtils.equals(quantity.getFormat(),"Gi")){
            memUse += Long.parseLong(quantity.getAmount())*1024*1024;
        }

        if (StringUtils.isEmpty(quantity.getFormat())){
            memUse += Long.parseLong(quantity.getAmount())/(1024*1024);
        }

        return memUse;
    }
    
}
